package com.yb.web.shoper;

/**
 * 评价
 */
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;

import com.opensymphony.xwork2.ActionContext;
import com.yb.base.web.BaseAction;
import com.yb.bussiness.announcement.AnnouncementService;
import com.yb.bussiness.announcement.AnnouncementServiceImpl;
import com.yb.bussiness.shoper.TgPingJiaService;
import com.yb.bussiness.shoper.TgPingJiaServiceImpl;
import com.yb.domain.goods.TgAnnouncement;
import com.yb.domain.goods.TgPingJia;
import com.yb.domain.member.TsSysuser;
import com.yb.util.comm.Contants;
import com.yb.util.comm.DateTimeUtil;
import com.yb.util.comm.PojoPropertiesUtil;
import com.yb.util.comm.RequestUtil;

public class TgPingJiaAction extends BaseAction {
    private TgPingJiaService service;
    
    private TgPingJia pg;
    
    private int pageNo;
    
    private int limit;
    
    private int total;
    
    private int flag;
    
    @SuppressWarnings("unchecked")
    private List rows;
    
    private AnnouncementService annoucementService;
    
    public TgPingJia getPg() {
        return pg;
    }
    
    public void setPg(TgPingJia pg) {
        this.pg = pg;
    }
    
    public TgPingJiaAction() {
        service = new TgPingJiaServiceImpl();
        annoucementService = new AnnouncementServiceImpl();
    }
    
    /**
     * 增加或者保存
     * 
     * @return
     */
    public String saveOrUpdate() {
        try {
            if (null == pg.getId()) {
                pg.setCreateTime(new Date());
            }
            boolean isTrue = service.saveOrUpdateTgPingJia(pg);
            if (isTrue)
                response.getWriter().print("<script>parent.addResult('1')</script>");
            else
                response.getWriter().print("<script>parent.addResult('2')</script>");
        }
        catch (Exception e) {
            e.printStackTrace();
            log.error("TgPingJiaAction 评价保存失败", e);
            try {
                response.getWriter().print("<script>parent.addResult('2')</script>");
            }
            catch (IOException e1) {
                e1.printStackTrace();
            }
            return null;
        }
        return null;
    }
    
    /**
     * 业主修改或者保存
     * 
     * @return
     */
    public String save() {
        try {
            String id = RequestUtil.getParam(request, "pg.id", "");
            TgPingJia tpPingJia = service.findById(id);
            if (tpPingJia != null) {
                PojoPropertiesUtil util = PojoPropertiesUtil.getInstance();
                pg = (TgPingJia)util.copyProperties(tpPingJia, pg);
            }
            pg.setCreateTime(new Date());
            boolean isTrue = service.saveOrUpdateTgPingJia(pg);
            if (isTrue)
                return "showlist";
            return "error";
            
        }
        catch (Exception e) {
            e.printStackTrace();
            log.error("TgPingJiaAction save", e);
            return "error";
            
        }
        
    }
    
    /**
     * 曝光维权
     */
    public String findAllMesage() {
        try {
            HashMap<String, String> paraMap = new HashMap<String, String>();
            // List<TgPingJia> list = service.searchByParaMap(paraMap);
            paraMap.put("type", "1");
            int haoCount = service.count(paraMap);
            paraMap.remove("type");
            paraMap.put("type", "2");
            int chaCount = service.count(paraMap);
            // ActionContext.getContext().put("pingjialist", list);
            request.setAttribute("haoCount", haoCount);
            request.setAttribute("chaCount", chaCount);
            TgAnnouncement announcement = annoucementService.findTgByType("2");
            ActionContext.getContext().put("announcement", announcement);
            return "list";
        }
        catch (Exception e) {
            e.printStackTrace();
            log.error(" 曝光维权 查询报错", e);
            return "error";
        }
    }
    
    /**
     * 分页查询
     */
    public String findByFenyeMesage() {
        try {
            if (pageNo == 0)
                pageNo = 1;
            if (limit == 0)
                limit = 20;
            HashMap<String, String> paraMap = new HashMap<String, String>();
            String type = RequestUtil.getParam(request, "type", "");
            paraMap.put("type", type);
            paraMap.put("page", String.valueOf(pageNo));
            paraMap.put("limit", String.valueOf(limit));
            List<TgPingJia> list = service.searchByParaMap(paraMap);
            response.getWriter().print(JSONArray.fromObject(list));
        }
        catch (Exception e) {
            e.printStackTrace();
            log.error(" 曝光维权 分页查询报错", e);
            return "error";
        }
        return null;
    }
    
    /**
     * 查询曝光维权详情页
     */
    public String findByMess() {
        try {
            String id = RequestUtil.getParam(request, "id", "");
            TgPingJia tpPingJia = service.findById(id);
            ActionContext.getContext().put("tpPingJia", tpPingJia);
            TgAnnouncement announcement = annoucementService.findTgByType("2");
            ActionContext.getContext().put("announcement", announcement);
            return "showdetail";
        }
        catch (Exception e) {
            e.printStackTrace();
            log.error(" 曝光维权 查询findByMess报错", e);
            return "error";
        }
    }
    
    /**
     * 业主修改评价记录
     */
    public String findModify() {
        try {
            String id = RequestUtil.getParam(request, "id", "");
            TgPingJia tpPingJia = service.findById(id);
            ActionContext.getContext().put("tpPingJia", tpPingJia);
            return "modify";
        }
        catch (Exception e) {
            e.printStackTrace();
            log.error(" 曝光维权 查询findByMess报错", e);
            return "error";
        }
    }
    
    /**
     * 业主评价信息查询
     * 
     * @return
     */
    public String search() {
        String page = RequestUtil.getParam(request, "page", "");
        String row = RequestUtil.getParam(request, "rows", "");
        TsSysuser tsSysuser = (TsSysuser)RequestUtil.getLoginUserFromSession(request, Contants.SESSION_USER);
        HashMap<String, String> parameMap = new HashMap<String, String>();
        parameMap.put("page", page);
        parameMap.put("limit", row);
        parameMap.put("yeid", tsSysuser.getId().toString());
        List<TgPingJia> list = service.searchRecord(parameMap);
        int count = service.recordcount(parameMap);
        if (list != null) {
            total = count;
            rows = list;
        }
        return Contants.JSONSTRING_RESULT_NAME;
    }
    
    public String pingjiaManage() {
        try {
            String page = RequestUtil.getParam(request, "page", "1");
            String row = RequestUtil.getParam(request, "rows", "10");
            String username = RequestUtil.getParam(request, "username", "");
            String startTime = RequestUtil.getParam(request, "startTime", "");
            String endTime = RequestUtil.getParam(request, "endTime", "");
            String reply = RequestUtil.getParam(request, "reply", "");
            String type = RequestUtil.getParam(request, "type", "");
            TgPingJia formBean = new TgPingJia();
            TsSysuser tsSysuser = (TsSysuser)RequestUtil.getLoginUserFromSession(request, Contants.SESSION_USER);
            formBean.setBrandId(new Integer(tsSysuser.getId() + ""));
            formBean.setUsername(username);
            formBean.setReply(reply);
            formBean.setStartTime(startTime);
            formBean.setEndTime(endTime);
            formBean.setType(type);
            Map<String, Object> map = service.findPj(formBean, page, row);
            rows = (List<TgPingJia>)map.get("list");
            total = Integer.parseInt(map.get("total").toString());
        }
        catch (Exception e) {
            log.error("评价信息出错", e);
        }
        return Contants.JSONSTRING_RESULT_NAME;
    }
    
    /*
     * 跳转评价信息页面
     */
    public String topingjiaInfo() {
        long id = RequestUtil.getParam(request, "id", 0l);
        pg = service.read(id);
        pg.setCreateTimeStr(DateTimeUtil.switchDateToString(pg.getCreateTime(), "yyyy-MM-dd HH:mm:ss"));
        pg.setReplyTimeStr(DateTimeUtil.switchDateToString(pg.getReplyTime(), "yyyy-MM-dd HH:mm:ss"));
        return "pingjiainfo";
    }
    
    /**
     * 回复
     * 
     * @return
     */
    public String reply() {
    	String flag = RequestUtil.getParam(request, "flag", "");
        try {
        	if("3".equals(flag)){
        		pg.setCreateTime(new Date());
        		pg.setReply(null);
        		pg.setReplyTime(null);
        	}
        	else
        		pg.setReplyTime(new Date());
            service.saveOrUpdateTgPingJia(pg);
        }
        catch (Exception e) {
            e.printStackTrace();
            log.error("TgPingJiaAction 回复失败", e);
        }
        if("3".equals(flag))
        	return "yzPage";
        return "pjmanage";
    }
    
    public String replyStage(){
    	PrintWriter out = null;
    	try{
    		out = response.getWriter();
    		Long id = RequestUtil.getParam(request, "id", 0l);
    		String content = RequestUtil.getParam(request, "content", "");
    		service.saveReply(id, content);
    		out.write("1");
    	}catch (Exception e) {
    		log.error("商家回复评论失败（商家页面快捷回复）", e);
    		out.write("2");
    	}
    	return null;
    }
    
	public String delete() {
		String idString = RequestUtil.getParam(request, "idString", "");
		String iDString = idString == null ? "" : idString;
        try {
			String[] iDArray = iDString.split("_");
			if (iDArray.length > 0) {
			    for (int i = 0; i < iDArray.length; i++) {
			    	service.deleteByIdString(idString);
			    }
			}
			message = "删除成功";
		} catch (Exception e1) {
			message = "删除失败";
		}
		

		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = null;
		StringBuffer sb = new StringBuffer("[");
		sb.append("{\"message\":\"" + message + "\"" + "}");
		sb.append("]");

		try {
			out = response.getWriter();
			out.println(sb.toString());
		} catch (IOException e) {

			e.printStackTrace();
		}
		return null;
	}
    public int getPageNo() {
        return pageNo;
    }
    
    public void setPageNo(int pageNo) {
        this.pageNo = pageNo;
    }
    
    public int getLimit() {
        return limit;
    }
    
    public void setLimit(int limit) {
        this.limit = limit;
    }
    
    public int getTotal() {
        return total;
    }
    
    public void setTotal(int total) {
        this.total = total;
    }
    
    public List getRows() {
        return rows;
    }
    
    public void setRows(List rows) {
        this.rows = rows;
    }

    public int getFlag() {
        return flag;
    }

    public void setFlag(int flag) {
        this.flag = flag;
    }
    
    
    
}
